<template>
  <div class="admin-edit padding-top">
    <el-table
      :data="aList"
      border
      style="width: 100%"
      ref="table"
      :height="oHeight"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
    >
      <el-table-column fixed prop="admin_id" label="ID" key="admin_id"></el-table-column>
      <el-table-column
        prop="admin_name"
        label="管理员名称"
        key="admin_name"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column fixed="right" align="center" label="操作" key="operate">
        <template slot-scope="scope">
          <el-button
            class="operateBtn"
            type="text"
            @click="edit(scope.row.admin_id, scope.row.admin_name)"
          >编辑</el-button>
          <el-button
            class="operateBtn"
            type="text"
            @click="deleter(scope.row.admin_id, scope.row.depart_id)"
          >|删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑管理员" :visible.sync="addNew" class="admin-control-edit">
      <el-form :model="adminForm" label-position="left" :rules="rule1" ref="ruleForm1">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="admin_name">
          <el-input v-model.trim="adminForm.admin_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="admin_password">
          <el-input v-model.trim="adminForm.admin_password" type="password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" :label-width="formLabelWidth" prop="confirm_password">
          <el-input v-model.trim="adminForm.confirm_password" type="password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addNew = false">取 消</el-button>
        <el-button type="primary" @click="addNewAdmin">确 定</el-button>
      </div>
    </el-dialog>
    <!-- <pagination
      :arr="aList"
      :total="total"
      :currentPage="currentPage"
      :handleSizeChange="handleSizeChange"
      :handleCurrentChange="handleCurrentChange"
    ></pagination>-->
  </div>
</template>

<script>
import pagination from "@/components/pagination/pagination.vue";
import { resize } from "@/common/js/height.js";

export default {
  props: {},
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.adminForm.admin_password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      oHeight: 800,
      aList: [],
      total: 1,
      currentPage: 1,
      formLabelWidth: "80px",
      depart_id: "",
      addNew: false,
      adminForm: {
        admin_id: "",
        admin_name: "",
        admin_password: "",
        confirm_password: ""
      },
      rule1: {
        admin_name: {
          required: true,
          message: "请填写用户名",
          trigger: "blur"
        },
        admin_password: {
          required: true,
          message: "请填写密码",
          trigger: "blur"
        },
        confirm_password: {
          required: true,
          validator: validatePass2,
          trigger: "blur"
        }
      }
    };
  },
  components: {
    pagination
  },
  props: ["changed"], //父子组件传值---新增管理员
  created() {
    this.depart_id = this.$store.state.couple.id;
    this.getList();
    resize(this);
  },
  methods: {
    // 获取管理员列表
    getList() {
      this.$http
        .post("/admin/depart/index.php?act=departManage&op=adminList", {
          depart_id: this.depart_id
        })
        .then(res => {
          if (res.code == "1") {
            this.aList = res.data.list;
          }
        });
    },
    // 点击编辑管理员
    edit(id, name) {
      this.addNew = true;
      this.adminForm.admin_name = name;
      this.adminForm.admin_id = id;
    },
    // 编辑管理员
    addNewAdmin() {
      this.$refs.ruleForm1.validate(valid => {
        if (valid) {
          this.$http
            .post(
              "/admin/auth/index.php?act=adminManage&op=addAdmin",
              this.adminForm
            )
            .then(
              res => {
                if (res.code == 1) {
                  this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "success"
                  });
                  this.addNew = false;
                } else {
                  this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "error"
                  });
                }
                this.getList();
              },
              err => {
                this.$message.error("获取数据失败！");
              }
            );
        } else {
          return false;
        }
      });
    },
    // 删除管理员
    deleter(id) {
      this.$confirm("确认删除？")
        .then(_ => {
          this.$http
            .post("/admin/depart/index.php?act=departManage&op=delAdmin", {
              admin_id: id,
              depart_id: this.depart_id
            })
            .then(
              res => {
                if (res.code == 1) {
                  this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "success"
                  });
                } else {
                  this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "error"
                  });
                }
                this.getList();
              },
              err => {
                this.$message({
                  showClose: true,
                  message: res.msg,
                  type: "error"
                });
              }
            );
          done();
        })
        .catch(_ => {});
    },
    // 改变当前页
    handleCurrentChange: function(val) {
      this.currentPage = val;
      /*请求数据*/
      this.getList();
    },
    //分组一页多少数据
    handleSizeChange(val) {
      this.currentPage = 1;
      this.searchObj.page = 1;
      this.searchObj.pageSize = val;
      /*请求数据*/
      this.getList();
    }
  },
  watch: {
    changed(n, o) {
      this.getList();
    },
    addNew(n, o) {
      if (!n) {
        this.$refs.ruleForm1.resetFields();
      }
    }
  }
};
</script>

<style scoped lang="scss">
</style>
